package com.powerbank.usercenter.controller;

import com.powerbank.common.result.Result;
import com.powerbank.usercenter.dto.IdentityVerifyDTO;
import com.powerbank.usercenter.service.IdentityService;
import com.powerbank.usercenter.vo.IdentityInfoVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

/**
 * 用户身份认证控制器
 */
@Tag(name = "身份认证", description = "用户实名认证相关接口")
@Slf4j
@RestController
@RequestMapping("/identity")
@Validated
public class IdentityController {

    @Autowired
    private IdentityService identityService;

    /**
     * 提交实名认证
     */
    @Operation(summary = "提交实名认证", description = "用户提交实名认证信息")
    @PostMapping("/verify")
    public Result<Void> submitIdentityVerify(@Valid @RequestBody IdentityVerifyDTO verifyDTO,
                                            @RequestHeader("X-User-Id") Long userId) {
        log.info("提交实名认证: userId={}, realName={}", userId, verifyDTO.getRealName());
        identityService.submitVerify(userId, verifyDTO);
        return Result.success();
    }

    /**
     * 获取认证状态
     */
    @Operation(summary = "获取认证状态", description = "获取用户实名认证状态")
    @GetMapping("/{userId}/status")
    public Result<IdentityInfoVO> getIdentityStatus(
            @Parameter(description = "用户ID") @PathVariable @NotNull Long userId) {
        log.info("获取认证状态: userId={}", userId);
        IdentityInfoVO identityInfo = identityService.getIdentityStatus(userId);
        return Result.success(identityInfo);
    }

    /**
     * 获取当前用户认证状态
     */
    @Operation(summary = "获取当前用户认证状态", description = "获取当前登录用户的实名认证状态")
    @GetMapping("/current/status")
    public Result<IdentityInfoVO> getCurrentIdentityStatus(@RequestHeader("X-User-Id") Long userId) {
        log.info("获取当前用户认证状态: userId={}", userId);
        IdentityInfoVO identityInfo = identityService.getIdentityStatus(userId);
        return Result.success(identityInfo);
    }

    /**
     * 重新认证
     */
    @Operation(summary = "重新认证", description = "重新提交实名认证（认证失败后）")
    @PostMapping("/reverify")
    public Result<Void> reVerify(@Valid @RequestBody IdentityVerifyDTO verifyDTO,
                                @RequestHeader("X-User-Id") Long userId) {
        log.info("重新认证: userId={}, realName={}", userId, verifyDTO.getRealName());
        identityService.reVerify(userId, verifyDTO);
        return Result.success();
    }
}